using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Business;
using Entity;

public partial class EmployeePage_StoreManage : System.Web.UI.Page
{
    eStores store = new eStores();
    DataTable dt_City;
    protected void Page_Load(object sender, EventArgs e)
    {
        store.StoreID = -1;
        dt_City = Business.bCity.getAllCity();

        if (!IsPostBack)
        {
            DataTable dt = bStores.getAllStores();
            gvStore.DataSource = dt;
            gvStore.DataBind();

            dlCity.DataSource = dt_City;
            dlCity.DataTextField = "CityName";
            dlCity.DataValueField = "CityID";

            dlCity.DataBind();
        }
        btDelete.Attributes.Add("onclick", "return confirm('Are you sure you want to delete this store')");    
    }

    protected void dlCity_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataTable dt = bStores.getAllStores();
        DataView df = dt.DefaultView;
        df.RowFilter = "CityID='" + dlCity.SelectedValue + "'";
        gvStore.DataSource = df;
        gvStore.DataBind();
    }

    protected void gvStore_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Detail")
        {
            btAdd.Text = "Update";
            int stID = int.Parse(e.CommandArgument.ToString());
            store = bStores.getStoresByID(stID);
            resetForm();
            MultiView1.ActiveViewIndex = 1;
        }
    }
    void resetForm()
    {
        if (btAdd.Text == "Update")
        {
            txtID.Text = store.StoreID.ToString();
            txtName.Text = store.StoreName;
            txtAddress.Text = store.StoreAddress;

            dlCity.SelectedValue = store.CityID.ToString();
            btDelete.Visible = true;            
        }
        else
        {
            txtID.Text = "";
            txtName.Text = "";
            txtAddress.Text = "";
            txtPhone.Text = "";

            btDelete.Visible = false;
            
        }
        lbMesss.Text = "";
    }

    protected void btBack_Click(object sender, EventArgs e)
    {
        MultiView1.ActiveViewIndex = 0;
    }
    protected void btAdd_Click(object sender, EventArgs e)
    {
        int CityID;
        if (dlCity.SelectedItem.Text == "Hanoi" || dlCity.SelectedItem.Text == "Ho Chi Minh City")
        {
            CityID = int.Parse(dlCity.SelectedValue);
        }
        else
        {
            CityID = -1;
        }
        if(btAdd.Text == "Update")
        {
            int stID = int.Parse(txtID.Text);
            store = bStores.getStoresByID(stID);
            store.StoreName =txtName.Text;
            store.StoreAddress = txtAddress.Text;
            store.StorePhone = txtPhone.Text;
            dlCity.SelectedValue = store.CityID.ToString();
            bStores.updateStores(store);
            lbMesss.Text = "Has update";

            DataTable dt = bStores.getAllStores();
            gvStore.DataSource = dt;
            gvStore.DataBind();
        }
        else{
            store.StoreName = txtName.Text;
            store.StoreAddress = txtAddress.Text;
            store.StorePhone = txtPhone.Text;
            store.CityID = int.Parse(dlCity.SelectedValue);
            lbMesss.Text = "Has Added!";
            DataTable dt = bStores.getAllStores();
            gvStore.DataSource = dt;
            gvStore.DataBind();
            MultiView1.ActiveViewIndex = 0;
        }
    }

    protected void btReset_Click(object sender, EventArgs e)
    {
        if (btAdd.Text == "Update")
        {
            int stID = int.Parse(txtID.Text);
            store = bStores.getStoresByID(stID);
        }
        resetForm();
    }

    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        btAdd.Text = "Add New";
        resetForm();
        MultiView1.ActiveViewIndex = 1;
    }
    protected void btDelete_Click(object sender, EventArgs e)
    {
        int stID = int.Parse(txtID.Text);
        bStores.deleteStores(stID);

        DataTable dt = bStores.getAllStores();
        gvStore.DataSource = dt;
        gvStore.DataBind();
        MultiView1.ActiveViewIndex = 0;
    }

}
